package cyy.example.com.facotry.net.helper;

import android.support.annotation.NonNull;
import android.util.Log;

import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.L;
import com.example.com.common.utils.ToastUtil;
import com.google.gson.JsonSyntaxException;

import java.net.ConnectException;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.R;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.net.RspCode;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.HttpException;
import retrofit2.Response;

/**
 * Created by ChenYiYao on 2017/11/19.
 */

public class RspCallback<T extends BaseEntity> implements Callback<T> {

    final DataSource.Callback<T> callback;
    private int reqId;

    public RspCallback(DataSource.Callback<T> callback, int reqId) {
        this.callback = callback;
        this.reqId = reqId;
    }

    @Override
    public void onResponse(Call<T> call,
                           Response<T> response) {
        if (call.isCanceled()) {
            return;//请求已取消
        }

        // 请求成功返回
        // 从返回中得到我们的全局Model，内部是使用的Gson进行解析
        if (response.body() instanceof BaseEntity) {
            BaseEntity body = response.body();
            if (callback == null) {

            } else if (body == null) {
                //TODO: 还可优化，判断内部data数据是否为空等
                //实体为空
                Factory.decodeRspCode(RspCode.SYSTEM_ERROR);
                callback.onDataNotAvailable(Factory.app().getString(R.string.data_rsp_error), reqId);
            } else if (body.code.equals(RspCode.ERROR_ACCOUNT_TOKEN)) {
                //token失效
                Factory.decodeRspCode(RspCode.ERROR_ACCOUNT_TOKEN);
            } else if(RspCode.ERROR_USER_NOT_FIND.equals(body.code)){
                Factory.decodeRspCode(RspCode.ERROR_USER_NOT_FIND);

            }else if("500".equals(body.code)){
                ToastUtil.showToastShort(Factory.app(), body.msg);
            }else{
                callback.onDataLoaded(response.body(), reqId);
            }
        } else {
            if (response.body() == null) {
                //实体为空
                Factory.decodeRspCode(RspCode.SYSTEM_ERROR);
                if (callback != null) {
                    callback.onDataNotAvailable(Factory.app().getString(R.string.data_rsp_error), reqId);
                }

            } else if (callback != null) {
                callback.onDataLoaded(response.body(), reqId);
            }
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        String msg = null;
        if (call.isCanceled()) {
            //取消请求
            L.e("----------------------");
            L.e("-----------取消请求-----------");
            L.e("----------------------");
        } else {
            // 网络请求失败
            if (t instanceof JsonSyntaxException) {
                msg = Factory.app().getResources().getString(R.string.data_parsing_error);
            } else if (t instanceof ConnectException) {
                msg = networkUnavailHandle();
            } else if (t instanceof HttpException) {
                HttpException exception = (HttpException) t;
                L.e("----------- exception.code()-----------" + exception.code());
                msg = Factory.app().getResources().getString(R.string.network_error);
            } else {
                msg = Factory.app().getResources().getString(R.string.network_error);
            }
            if (callback != null) {
                callback.onDataNotAvailable(msg, reqId);
            }
            L.e("------网络请求失败------");
            L.e(call.request().url() + "");
            L.e(t.toString());
            L.e("----------------------");
        }
    }

    @NonNull
    private String networkUnavailHandle() {
        String msg;
        msg = Factory.app().getResources().getString(R.string.network_unavailable);
        return msg;
    }

}